#  Copyright (C) 2014-2022 Yubico AB - See COPYING

SUBDIRS = . pamu2fcfg tests

if ENABLE_MAN
SUBDIRS += man
endif

if ENABLE_FUZZING
SUBDIRS += fuzz
endif

ACLOCAL_AMFLAGS = -I m4

AM_CFLAGS = $(CWFLAGS) $(CSFLAGS)
AM_CPPFLAGS = $(LIBFIDO2_CFLAGS) $(LIBCRYPTO_CFLAGS)
if ENABLE_FUZZING
AM_CPPFLAGS += -fsanitize=fuzzer-no-link
endif

noinst_LTLIBRARIES = libmodule.la
libmodule_la_SOURCES = pam-u2f.c
libmodule_la_SOURCES += b64.c b64.h
libmodule_la_SOURCES += debug.c debug.h
libmodule_la_SOURCES += drop_privs.h
libmodule_la_SOURCES += expand.c
libmodule_la_SOURCES += explicit_bzero.c
libmodule_la_SOURCES += util.c util.h
libmodule_la_LIBADD = -lpam $(LIBFIDO2_LIBS) $(LIBCRYPTO_LIBS)

pampluginexecdir = $(PAMDIR)
pampluginexec_LTLIBRARIES = pam_u2f.la
pam_u2f_la_SOURCES =
pam_u2f_la_LIBADD = libmodule.la
pam_u2f_la_LDFLAGS = -module -avoid-version

if !ENABLE_FUZZING
pam_u2f_la_LDFLAGS += -export-symbols $(srcdir)/export.sym
else
pam_u2f_la_LDFLAGS += -export-symbols $(srcdir)/fuzz/export.sym
pam_u2f_la_SOURCES += fuzz/wrap.c
pam_u2f_la_LDFLAGS += -Wl,--wrap=asprintf
pam_u2f_la_LDFLAGS += -Wl,--wrap=strdup
pam_u2f_la_LDFLAGS += -Wl,--wrap=calloc
pam_u2f_la_LDFLAGS += -Wl,--wrap=malloc
pam_u2f_la_LDFLAGS += -Wl,--wrap=open
pam_u2f_la_LDFLAGS += -Wl,--wrap=close
pam_u2f_la_LDFLAGS += -Wl,--wrap=fdopen
pam_u2f_la_LDFLAGS += -Wl,--wrap=fstat
pam_u2f_la_LDFLAGS += -Wl,--wrap=read
pam_u2f_la_LDFLAGS += -Wl,--wrap=gethostname
pam_u2f_la_LDFLAGS += -Wl,--wrap=getline
pam_u2f_la_LDFLAGS += -Wl,--wrap=getpwnam_r
pam_u2f_la_LDFLAGS += -Wl,--wrap=getpwuid_r
pam_u2f_la_LDFLAGS += -Wl,--wrap=geteuid
pam_u2f_la_LDFLAGS += -Wl,--wrap=secure_getenv
pam_u2f_la_LDFLAGS += -Wl,--wrap=pam_get_user
pam_u2f_la_LDFLAGS += -Wl,--wrap=pam_get_item
pam_u2f_la_LDFLAGS += -Wl,--wrap=pam_modutil_drop_priv
pam_u2f_la_LDFLAGS += -Wl,--wrap=pam_modutil_regain_priv
pam_u2f_la_LDFLAGS += -Wl,--wrap=BIO_new
pam_u2f_la_LDFLAGS += -Wl,--wrap=BIO_write
pam_u2f_la_LDFLAGS += -Wl,--wrap=BIO_read
pam_u2f_la_LDFLAGS += -Wl,--wrap=BIO_ctrl
pam_u2f_la_LDFLAGS += -Wl,--wrap=BIO_new_mem_buf
pam_u2f_la_LDFLAGS += -Wl,--wrap=EC_KEY_new_by_curve_name
pam_u2f_la_LDFLAGS += -Wl,--wrap=EC_KEY_get0_group
pam_u2f_la_LDFLAGS += -Wl,--wrap=fido_dev_open
pam_u2f_la_LDFLAGS += -Wl,--wrap=fido_dev_info_manifest
endif

DEFS = -DDEBUG_PAM -DPAM_DEBUG @DEFS@

EXTRA_DIST = export.sym

# Release

# XXX: make distcheck happy with install location of pam_u2f.so
AM_DISTCHECK_CONFIGURE_FLAGS = --with-pam-dir='/$$(prefix)/$(PAMDIR)'

install-exec-hook:
	rm -f $(DESTDIR)$(pampluginexecdir)/pam_u2f.la
	chmod -f 644 $(DESTDIR)$(pampluginexecdir)/pam_u2f.so || true

uninstall-hook:
	rm -f $(DESTDIR)$(pampluginexecdir)/pam_u2f.so

indent:
	clang-format -i *.c *.h pamu2fcfg/*.c pamu2fcfg/*.h

ChangeLog:
	cd $(srcdir) && git2cl > ChangeLog

PROJECT = pam-u2f

release:
	@if test ! -d "$(YUBICO_WWW_REPO)"; then \
		echo "WWW repo not found!"; \
		echo "Make sure that YUBICO_WWW_REPO is set"; \
		exit 1; \
	fi
	@if test -z "$(KEYID)"; then \
		echo "Try this instead:"; \
		echo "  make release KEYID=[PGPKEYID]"; \
		echo "For example:"; \
		echo "  make release KEYID=2117364A"; \
		exit 1; \
	fi
	@head -5 $(srcdir)/NEWS | \
		grep -q "Version $(VERSION) .released `date -I`" || \
		(echo 'error: Update date/version in $(srcdir)/NEWS.'; exit 1)
	rm -f $(srcdir)/ChangeLog
	make ChangeLog distcheck
	gpg --detach-sign --default-key $(KEYID) $(PACKAGE)-$(VERSION).tar.gz
	gpg --verify $(PACKAGE)-$(VERSION).tar.gz.sig
	cd $(srcdir) && git push
	cd $(srcdir) && git tag -u $(KEYID) -m $(VERSION) $(PACKAGE)-$(VERSION)
	cd $(srcdir) && git push --tags
	$(YUBICO_WWW_REPO)/publish $(PROJECT) $(VERSION) $(PACKAGE)-$(VERSION).tar.gz*
